package com.airties.atof.client;

import com.airties.atof.data.AtofModuleConstants;
import com.airties.atof.utils.PropertyFileProvider;
import java.sql.Connection;
import java.sql.DriverManager;
import org.orm.PersistentException;
import org.orm.cfg.JDBCConnectionSetting;


/**
 *
 * @author nurhat
 */
public class loggerServer
{

    public static void main(String[] args)
    {
        try {
            System.out.println("Starting logging server....");
            loadUserDefinedDbDefinitions();
            if(isValidSqlConnection())
            {
                AtofApplicationContext.getLoggingContext();
                System.out.println("Started logging server");
                while (true) {
                    Thread.sleep(2000);

                }
            }else
                System.out.println("Can not login to mysql");
        } catch (Exception eee) {
            AtofApplicationContext.close();
        }
    }
      public static void loadUserDefinedDbDefinitions()
    {
        PropertyFileProvider propFile =
            new PropertyFileProvider();
        boolean isLoaded =
            propFile.loadPropertiesFile(AtofModuleConstants.ATOF_VENDOR_HOME +
                                        "/" +
                                        "database.props",
                                        false);
        if (isLoaded) {
            String dbUrl = propFile.getProperty("DATABASE_URL");
            String dbUserName = propFile.getProperty("DATABASE_USERNAME");
            String dbPassword = propFile.getProperty("DATABASE_PASSWORD");
            String dbName = propFile.getProperty("DATABASE_CATALOGNAME");
            JDBCConnectionSetting connectionSetting =
                new JDBCConnectionSetting();
            connectionSetting.setConnectionURL("jdbc:mysql://" + dbUrl + "/" +
                                               dbName+"_logs");
            connectionSetting.setDialect("org.hibernate.dialect.MySQLDialect");
            connectionSetting.setDriverClass("com.mysql.jdbc.Driver");

            connectionSetting.setUserName(dbUserName);
            connectionSetting.setPassword(dbPassword);
            try {
                com.airties.atof.orm.manager.AtofdbPersistentManager.
                    setJDBCConnectionSetting(connectionSetting);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        }

    }
     public static boolean isValidSqlConnection()
    {
        JDBCConnectionSetting settings = null;
        try {

            settings =
                com.airties.atof.orm.manager.AtofdbPersistentManager.
                getJDBCConnectionSetting();
        } catch (PersistentException ex) {
            ex.printStackTrace();
        }
        String dbUrl = "jdbc:mysql://localhost/atof_logs";
        String dbUserName = "root";
        String dbPassword = "12345";
        if (settings != null) {
            dbUrl = settings.getConnectionURL();
            dbUserName = settings.getUserName();
            dbPassword = settings.getPassword();
        }
        return checkMySqlConnection(dbUrl,
                                                    dbUserName,
                                                    dbPassword);


    }
      public static boolean checkMySqlConnection(String dbUrl,
                                               String dbUserName,
                                               String dbPassword)
    {

        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(dbUrl,
                                                           dbUserName,
                                                           dbPassword);
            con.close();
            System.out.println("Connection established to " + dbUrl + "...");
            return true;
        } catch (ClassNotFoundException ex) {
            System.out.println("com.mysql.jdbc.Driver class not found...");
        } catch (java.sql.SQLException e) {
            System.out.println("Mysql Connection couldn't be established to " +
                               dbUrl);
        }
        return false;
    }

}
